package com.xyf.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lop.open.api.sdk.LopException;
import com.xyf.sys.config.MiniprogramConfig;
import com.xyf.sys.domain.UserOrderEvent;
import com.xyf.sys.service.ILogisticService;
import com.xyf.sys.service.IOrderService;
import com.xyf.sys.util.R;
import com.xyf.sys.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author R-H
 * @Date 2024/3/31422
 * @Description
 */
@RestController
@RequestMapping("/logistic")
@Slf4j
@Api(tags = "物流相关")
public class LogisticController {

    @Autowired
    private ILogisticService logisticService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    @Qualifier(value = "clientConfig")
    private MiniprogramConfig.MiniConfig clientConfig;
    @Autowired
    @Qualifier(value = "manageConfig")
    private MiniprogramConfig.MiniConfig manageConfig;

    @ApiOperation("快递地址盲区检验")
    @PostMapping("/check")
    public Object routeCheck(@RequestBody LogisticCheck logisticCheckRequestVo) throws LopException {
        return R.ok(logisticService.check(logisticCheckRequestVo));
    }

    @ApiOperation("获取运单号")
    @PostMapping("/waybill")
    public Object waybill(@RequestBody OrderRequestVo requestVo) {
        return R.ok(orderService.getWaybillNo(requestVo.getOrderId(), requestVo.getUserId()));
    }


    @ApiOperation("面单库存查询")
    @PostMapping("/queryStock")
    public Object queryStock(@RequestBody QueryStock queryStock) {
        return R.ok(logisticService.queryStock(queryStock.getDeliveryId(), queryStock.getUserId()));
    }

    @ApiOperation("韵达运单订阅")
    @PostMapping("/subscribe")
    public Object subscribe(@RequestBody OrderRequestVo orderRequestVo) {
        return R.ok(logisticService.subscribeOrder(orderRequestVo.getOrderId(), orderRequestVo.getUserId()));
    }

    @ApiOperation("查询订单物流")
    @PostMapping("/query")
    public Object query(@RequestBody OrderRequestVo requestVo, HttpServletRequest request) {
        String appId = request.getHeader("APPID");
        String secret = appId.equals(clientConfig.getAppid()) ? clientConfig.getSecret() : manageConfig.getSecret();
        return R.ok(orderService.queryLogistic(requestVo.getOrderId(), requestVo.getOutTradeNo() , requestVo.getUserId(), appId, secret));
    }

    @ApiOperation("扫码列表")
    @PostMapping("/list")
    public Object list(@RequestBody ScanLogisticDataParam dataParam) {
        PageHelper.startPage(dataParam.getPageNum(), dataParam.getPageSize());
        List<OrderVo> list = orderService.eventList(dataParam.getUserId(), dataParam.getStatus(), dataParam.getEventType(), dataParam.getDeptId());
        return R.ok(new PageInfo(list));
    }

    @ApiOperation("扫码添加")
    @PostMapping("/add")
    public Object add(@RequestBody OrderRequestVo body) {
        return R.ok(orderService.addUserLogisticsOrder(body.getOutTradeNo(), body.getRoleId(), body.getUserId(), body.getDeptId()));
    }

    @ApiOperation("扫码删除")
    @PostMapping("/remove")
    public Object remove(@RequestBody UserOrderEvent body) {
        return R.ok(orderService.removeDictDataLogisticsOrder(body.getId(), body.getUserId(), body.getDeptId()));
    }

    @ApiOperation("确定揽收")
    @PostMapping("/confirm")
    public Object confirm(HttpServletRequest request) {
        return R.ok(orderService.submitUserDictData(request.getHeader("OPENID"), Long.parseLong(request.getHeader("DEPTID"))));
    }

    @ApiOperation("搜索")
    @PostMapping("/find")
    public Object findList(@RequestBody OrderRequestVo body) {
        return R.ok(orderService.findNonLogisticsOrderList(body));
    }

    @ApiOperation("扫码统计")
    @PostMapping("/count")
    public Object count(@RequestBody(required = false) UserEventStatisticParam body, HttpServletRequest request) {
        if (body == null){
            body = new UserEventStatisticParam();
            body.setUserId(request.getHeader("OPENID"));
            body.setDeptId(Long.parseLong(request.getHeader("DEPTID")));
        }
        return R.ok(orderService.userOrderEventCount(body.getUserId(), body.getDeptId()));
    }

    @ApiOperation("物流退款")
    @PostMapping("/refund")
    public Object refundLogistic(@RequestBody OrderRequestVo body) {
        return R.ok(orderService.updateRefundLogistic(body.getOrderId(), body.getOrderProductId(), body.getUserId(), body.getDeptId()));
    }

    @ApiOperation("获取运单号")
    @PostMapping("/createWaybill")
    public Object createOrderProductWaybill(@RequestBody OrderProductWaybillRequestVo requestVo) {
        orderService.getWaybillNo(requestVo.getOrderId(), requestVo.getOrderProductIds(), requestVo.getDeliveryId(), requestVo.getUserId());
        return R.ok(orderService.getDetail(requestVo.getOrderId()));
    }
}
